package com.climber.android.auth.ui

import android.text.TextUtils
import android.view.View
import com.climber.android.commonres.helper.onClick
import com.climber.android.commonres.ui.BaseMVPActivity
import com.climber.android.auth.R
import com.climber.android.auth.api.AuthService
import com.climber.android.commonres.app.service.OTPRequestType
import com.climber.android.commonres.util.UIHelper
import com.climber.android.commonsdk.api.APIError
import com.climber.android.commonsdk.api.entity.APIResponse
import com.climber.android.commonsdk.api.ApiObserver
import com.climber.android.commonsdk.api.GlobalErrorProcessor
import com.climber.android.commonsdk.util.APIDataHelper
import io.ganguo.library.mvp.http.RxSchedulers
import io.ganguo.library.mvp.ui.mvp.IPresenter
import io.ganguo.library.mvp.ui.mvp.di.component.AppComponent
import io.ganguo.library.mvp.util.RxLifecycleUtils
import io.ganguo.library.mvp.util.StringUtils
import io.ganguo.library.mvp.util.ToastHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.auth_activity_modify_passsword.*

/**
 * Created by Mikes at 2020-03-31 21:34
 */
class AuthModifyPasswordActivity : BaseMVPActivity<IPresenter>(){
    private val otpRequestType: OTPRequestType by lazy {
        OTPRequestType.valueOf(intent.getStringExtra("otpRequestType"))
    }

    private val phoneNumber : String by lazy {
        intent.getStringExtra("phoneNumber")
    }

    private val smsCode : String by lazy {
        intent.getStringExtra("smsCode")
    }

    override fun setupActivityComponent(appComponent: AppComponent) {
    }

    override fun getLayoutResourceId(): Int = R.layout.auth_activity_modify_passsword

    override fun initView() {
        when(otpRequestType){
            OTPRequestType.ModifyPassword ->{
                originPasswordView.visibility = View.VISIBLE
            }
            OTPRequestType.FindPassword ->{
                originPasswordView.visibility = View.GONE
            }
            else ->{

            }
        }
    }

    override fun initListener() {
        confirmBtn.onClick {
            val originPassword = originPasswordView.getInputContent()
            val newPassword = newPasswordView.getInputContent()
            val newPasswordAgain = newPasswordAgainView.getInputContent()

            when(otpRequestType){
                OTPRequestType.ModifyPassword ->{
                    if(TextUtils.isEmpty(originPassword)){
                        ToastHelper.toastMessage("请输入原密码")
                        return@onClick
                    }

                    if(TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(newPasswordAgain)){
                        ToastHelper.toastMessage("请输入新密码")
                        return@onClick
                    }

                    if(!StringUtils.equals(newPassword, newPasswordAgain)){
                        ToastHelper.toastMessage("2次新密码输入不一致，请检查。")
                        return@onClick
                    }

                    doModifyPassword(oldPassword = originPassword, newPassword = newPassword)
                }
                OTPRequestType.FindPassword ->{
                    if(TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(newPasswordAgain)){
                        ToastHelper.toastMessage("请输入新密码")
                        return@onClick
                    }

                    if(!StringUtils.equals(newPassword, newPasswordAgain)){
                        ToastHelper.toastMessage("2次新密码输入不一致，请检查。")
                        return@onClick
                    }

                    doSetNewPassword(newPassword = newPassword)
                }
                else ->{

                }
            }
        }
    }

    override fun initData() {
    }

    /**
     * 修改密码
     */
    private fun doModifyPassword(oldPassword : String, newPassword : String){
        AuthService.authServiceAPI.issueModifyPassword(APIDataHelper.generateRequestBody(mapOf(
                "mobile" to phoneNumber,
                "code" to smsCode,
                "old_password" to oldPassword,
                "password" to newPassword
        )))
                .compose(RxSchedulers.networkRequest())
                .compose(GlobalErrorProcessor.processGlobalError(noticeErrorInfo = true))
                .doOnSubscribe { UIHelper.showLoading(this, null) }
                .subscribeOn(AndroidSchedulers.mainThread())
                .`as`(RxLifecycleUtils.bindLifecycle(this))
                .subscribe(object : ApiObserver<APIResponse>(){
                    override fun success(apiResponse: APIResponse) {
                        UIHelper.hideLoading()

                        ToastHelper.toastMessage("修改密码成功！请使用新密码重新登录")
                        finish()
                    }

                    override fun dealError(apiError: APIError) {
                        super.dealError(apiError)
                        UIHelper.hideLoading()

                        APIDataHelper.showAPIError(apiError)
                    }
                })
    }

    /**
     * 设置新密码
     */
    private fun doSetNewPassword(newPassword : String){
        AuthService.authServiceAPI.issueForgetPassword(APIDataHelper.generateRequestBody(mapOf(
                "mobile" to phoneNumber,
                "code" to smsCode,
                "password" to newPassword
        )))
                .compose(RxSchedulers.networkRequest())
                .compose(GlobalErrorProcessor.processGlobalError(noticeErrorInfo = true))
                .doOnSubscribe { UIHelper.showLoading(this, null) }
                .subscribeOn(AndroidSchedulers.mainThread())
                .`as`(RxLifecycleUtils.bindLifecycle(this))
                .subscribe(object : ApiObserver<APIResponse>(){
                    override fun success(apiResponse: APIResponse) {
                        UIHelper.hideLoading()

                        ToastHelper.toastMessage("重置密码成功！请使用新密码重新登录")
                        finish()
                    }

                    override fun dealError(apiError: APIError) {
                        super.dealError(apiError)
                        UIHelper.hideLoading()
                    }
                })
    }
}